const EnemySpawner = require("../Enemy/EnemySpawner");
var EnemyController = require("EnemyController");
var Player = require("PlayerController");

var VisibleRange = cc.Class({
    extends: cc.Component,

    properties: {
        isDetecting: cc.Boolean,
        detectTime: 0.5,

        enemiesInVisibleRange: {
            default: [],
            type: [EnemyController],
            visible: false,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.timer = this.detectTime;
        this.closestEnemyPosition ;
        this.closestEnemyDegree = 0;
    },

    start () {

    },


	onCollisionEnter (other, self) {
        if(this.isDetecting === true){
            if (other.node.group == 'Enemy') {
                this.enemiesInVisibleRange.push(other.node.getComponent("EnemyController"));
            }
        }

	},

	onCollisionExit (other, self) {
		if (this.isDetecting == true) {
			if (other.node.group == 'Enemy') {
				this.enemiesInVisibleRange.splice(this.enemiesInVisibleRange.indexOf(other.node.getComponent("EnemyController")), 1);
			}
		}
	},

    update (dt) {
        this.timer -= dt;
        if(this.timer <= 0 && this.enemiesInVisibleRange.length){
            let closestDistance;
            let playerPosition = Player.instance.node.position;
            for (let index = 0; index < this.enemiesInVisibleRange.length; index++) {
                    if(!index){
                        closestDistance = cc.Vec2.distance(this.enemiesInVisibleRange[index].node.position ,playerPosition);
                        this.closestEnemyPosition = this.enemiesInVisibleRange[index].node.position;
                        
                    }
                    else{
                        if(closestDistance > cc.Vec2.distance(this.enemiesInVisibleRange[index].node.position ,playerPosition)){
                            closestDistance = cc.Vec2.distance(this.enemiesInVisibleRange[index].node.position ,playerPosition);
                            this.closestEnemyPosition = this.enemiesInVisibleRange[index].node.position;
                        }
                    }
                }


            this.closestEnemyDegree = this.getDrgree(this.closestEnemyPosition.x - playerPosition.x, this.closestEnemyPosition.y - playerPosition.y)
         
            this.timer = this.detectTime;
        }
    },
    


    getDrgree(x, y) {
        let drgree = 0;
        if (y === 0 && x === 0) {
            drgree = null;
        }
        else if (y === 0) {
            if (x > 0) {
                drgree = 90;
            }
            else if (x < 0) {
                drgree = 270;
            }
        }
        else if (x === 0) {
            if (y > 0) {
                drgree = 0;
            }
            else if (y < 0) {
                drgree = 180;
            }
        }
        else {
            let tanRadians = x / y;
            if (x > 0 && y > 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians));
            }
            else if (x > 0 && y < 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians)) + 180;
            }
            else if (x < 0 && y < 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians)) + 180;
            }
            else if (x < 0 && y > 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians)) + 360;
            }
        }
        return drgree;
    },
});

module.exports = VisibleRange;
